package org.itboys.core.controller.front.order;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.dro.order.SundaySendOrderDro;
import org.itboys.core.dto.order.SundaySendOrderDto;
import org.itboys.core.service.mysql.front.SundayFrontSendOrderService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2018/12/5
 * 描述：配送订单
 */
@Controller
@RequestMapping("/sunday/mobile/sendOrder")
@Api(value = "sendOrder", description = "配送订单")
public class SundayMobileSendOrderController extends BaseController {
    @Autowired
    private SundayFrontSendOrderService sendOrderService;

    @RequestMapping(value = "/findOne", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端,供应商端】订单详情", notes = "订单详情的接口")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        SundaySendOrderDto result = sendOrderService.findOne(orderId);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/select", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端,供应商端】订单列表", notes = "订单列表的接口")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单状态 0,全部 1,待付款2,已付款3,已接单,4,已发盘,5,已收盘") @RequestParam(value = "status", required = true) Integer status,
            @ApiParam(value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(value = "排序参数。字段名称。默认为空。") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        List<SundaySendOrderDto> result = sendOrderService.selectOrders(request, memberId,status);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】创建配送订单",
            notes = "memberId:用户id type:1，日租  2，月租  3，季租 time:时间  多少天 getType:取托方式 1.送货上门2.租户自取 getTime:取托时间" +
                    "sendProvinceId:送托省份id sendProvince:送托省份 sendCityId:送托城市id sendCity:送托城市 sendDistrictId:送托区域id sendDistrict:送托区域" +
                    "sendAddress:送托地址 recycleDate:预计回收时间 recycleProvinceId:回收省份id recycleProvince:回收省份 recycleCityId:回收城市id recycleCity:回收城市 recycleDistrictId:回收区域id" +
                    "recycleDistrict:回收区域 recycleAddress:回收地址 models:订单模板")
    public void create(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "请求参数json", required = true) @RequestBody SundaySendOrderDro sendOrder
    ) throws Exception {
        sendOrder.validate();
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.createOrder(memberId,sendOrder);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/pay", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】支付配送订单", notes = "支付配送订单的接口")
    public void pay(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.pay(orderId, memberId);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/cancelOrder", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】取消配送订单", notes = "取消配送订单的接口")
    public void cancelOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.cancelOrder(orderId, memberId);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/confirm", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】确认配送订单", notes = "确认配送订单的接口")
    public void confirm(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.confirmOrder(orderId, memberId);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/send", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】配送订单发盘", notes = "配送订单发盘的接口")
    public void send(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "托盘id集合  例：1,2,3") @RequestParam(value = "trayIds", required = true) List<Long> trayIds
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.sendOder(orderId, memberId, trayIds);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/scan", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】配送订单扫码校检", notes = "配送订单扫码校检的接口")
    public void scan(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "模板id") @RequestParam(value = "modelId", required = true) Long modelId,
            @ApiParam(value = "托盘id") @RequestParam(value = "trayId", required = true) Long trayId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        sendOrderService.scan(orderId, memberId,modelId,trayId);
        ResponseMessage.success();
    }


    @RequestMapping(value = "/receive", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】配送订单收盘", notes = "配送订单收盘的接口")
    public void receive(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.receiveOder(orderId, memberId);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/renew", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】配送订单续费", notes = "配送订单续费的接口")
    public void renew(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "续费时间") @RequestParam(value = "renewTime", required = true) Long renewTime,
            @ApiParam(value = "回收省份id") @RequestParam(value = "recycleProvinceId", required = true) Long recycleProvinceId,
            @ApiParam(value = "回收省份") @RequestParam(value = "recycleProvince", required = true) String recycleProvince,
            @ApiParam(value = "回收城市id") @RequestParam(value = "recycleCityId", required = true) Long recycleCityId,
            @ApiParam(value = "回收城市") @RequestParam(value = "recycleCity", required = true) String recycleCity,
            @ApiParam(value = "回收区域id") @RequestParam(value = "recycleDistrictId", required = true) Long recycleDistrictId,
            @ApiParam(value = "回收区域") @RequestParam(value = "recycleDistrict", required = true) String recycleDistrict,
            @ApiParam(value = "回收地址") @RequestParam(value = "recycleAddress", required = true) String recycleAddress

    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.renew(orderId, memberId, renewTime, recycleProvinceId,
                recycleProvince, recycleCityId, recycleCity, recycleDistrictId, recycleDistrict, recycleAddress);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/renewPay", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】配送订单续费支付", notes = "配送订单续费支付的接口")
    public void renewPay(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "续费时间") @RequestParam(value = "renewTime", required = true) Long renewTime,
            @ApiParam(value = "回收省份id") @RequestParam(value = "recycleProvinceId", required = true) Long recycleProvinceId,
            @ApiParam(value = "回收省份") @RequestParam(value = "recycleProvince", required = true) String recycleProvince,
            @ApiParam(value = "回收城市id") @RequestParam(value = "recycleCityId", required = true) Long recycleCityId,
            @ApiParam(value = "回收城市") @RequestParam(value = "recycleCity", required = true) String recycleCity,
            @ApiParam(value = "回收区域id") @RequestParam(value = "recycleDistrictId", required = true) Long recycleDistrictId,
            @ApiParam(value = "回收区域") @RequestParam(value = "recycleDistrict", required = true) String recycleDistrict,
            @ApiParam(value = "回收地址") @RequestParam(value = "recycleAddress", required = true) String recycleAddress
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.renewPay(orderId, memberId, renewTime, recycleProvinceId,
                recycleProvince, recycleCityId, recycleCity, recycleDistrictId, recycleDistrict, recycleAddress);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/buyOut", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】配送订单买断", notes = "配送订单买断的接口")
    public void buyOut(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.buyOut(orderId, memberId);
        ResponseMessage.success(result);
    }


    @RequestMapping(value = "/buyOutPay", method = {RequestMethod.POST})
    @ApiOperation(value = "【租赁端】配送订单买断付款", notes = "配送订单买断付款的接口")
    public void buyOutPay(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.buyOutPay(orderId, memberId);
        ResponseMessage.success(result);
    }

    @RequestMapping(value = "/freezeBack", method = {RequestMethod.POST})
    @ApiOperation(value = "【供应商端】配送订单冻结回收", notes = "配送订单冻结回收的接口")
    public void freezeBack(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单id") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "托盘id集合  例：1,2,3") @RequestParam(value = "trayIds", required = true) List<Long> trayIds
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        SundaySendOrderDto result = sendOrderService.freezeBack(orderId, memberId, trayIds);
        ResponseMessage.success(result);
    }


}
